﻿namespace IBTrader.Indicator.CandleStick
{
    using IBTrader.Charts;
    using System;
    using System.Linq;

    class Ultimate : Base
    {
        private readonly Prices[] listBP, listTR;
        public Ultimate(Worker worker, int interval = 60, int size = 7) : base(worker, new ChartArgs { Group = 1 }, interval)
        {
            listBP = new[] { new Prices(size * 1), new Prices(size * 2), new Prices(size * 4) };
            listTR = new[] { new Prices(size * 1), new Prices(size * 2), new Prices(size * 4) };
        }
        protected override void Add(object _, Item item)
        {
            var last = (this as ILast).Last;
            var BP = Math.Min(item.Low, last.Close);
            foreach (var bp in listBP) bp.Add(BP);
            var TR = Math.Max(item.High, last.Close) - Math.Min(item.Low, last.Close);
            foreach (var tr in listTR) tr.Add(TR);
            var avg = Enumerable.Range(0, listBP.Length).Select(i => listTR[i].Sum / listBP[i].Sum).ToArray();
            var val = 100 * ((4 * avg[0]) + (2 * avg[1]) + avg[2]) / (4 + 2 + 1);
            OnHit(item, val);
        }
    }
}
